//以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
        $.validator.setDefaults({
            highlight: function (element) {
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
            },
            success: function (element) {
                element.closest('.form-group').removeClass('has-error').addClass('has-success');
            },
            errorElement: "span",
            errorPlacement: function (error, element) {
                if (element.is(":radio") || element.is(":checkbox")) {
                    error.appendTo(element.parent().parent().parent());
                } else {
                    error.appendTo(element.parent());
                }
            },
            errorClass: "help-block m-b-none",
            validClass: "help-block m-b-none"


        });

        //以下为官方示例
        $().ready(function () {
            // validate the comment form when it is submitted
            // validate signup form on keyup and submit
            var icon = "<i class='fa fa-times-circle'></i> ";
            $("#signupForm").validate({
                rules: {
                    webname: "required",
                    weblastname: "required",
                    webkey: {
                        required: true,
                    },
                   webdesc: {
                        required: true,
                    },
                    weblogo: {
                        required: true,
                    },
                    webcy: {
                        required: true,
                    },
                    webmail: {
                        required: true,
                        email:true,
                    },
                    webbq: {
                        required: true,
                    },
                    webicp: {
                        required: true,
                    },
                    webtj: {
                        required: true,
                    },
                    appid: {
                        required: true,
                    },
                    appsecret: {
                        required: true,
                    },
                    token: {
                        required: true,
                    },
                    aeskey: {
                        required: true,
                    },
                    domin: {
                        required: true,
                    },
                    gzresp: {
                        required: true,
                    },
                    description: {
                        required: true,
                    },
                    shorturl: {
                        required: true,
                    },
                    title: {
                        required: true,
                    },
                    img: {
                        required: true,
                    },
                    url: {
                        required: true,
                    },
                    lazyload: {
                        required: true,
                    },
                },
                messages: {
                    webname: icon + "请输入网站名称",
                    weblastname: icon + "请输入网站副标题",
                    webkey: {
                        required: icon + "请输入网站关键字",
                    },
                    webdesc: {
                        required: icon + "请输入网站描述",
                    },
                    weblogo: {
                        required: icon + "请输入logo地址",
                    },
                    webcy: {
                        required:icon + "请输入畅言代码",
                    },
                    webmail: {
                        required:icon + "请输入站长邮箱",
                        email:icon + "请输入正确的邮箱格式",
                    },
                    webbq: {
                        required:icon + "请输入底部版权",
                    },
                    webicp: {
                        required:icon + "请输入备案信息",
                    },
                    webtj: {
                        required:icon + "请输入统计代码",
                    },
                   appid: {
                        required:icon + "请输入APPID",
                    },
                    appsecret: {
                        required:icon + "请输入APP_Secret",
                    },
                    token: {
                        required:icon + "请输入Token",
                    },
                    aeskey: {
                        required:icon + "请输入AES_key",
                    },
                    domin: {
                        required:icon + "请输入您的域名",
                    },
                    gzresp: {
                        required:icon + "请输入关注后回复内容",
                    },
                    description: {
                        required:icon + "请输入自定义图文描述",
                    },
                    shorturl: {
                        required:icon + "请输入选择开启或关闭",
                    },
                    title: {
                        required:icon + "请输入无资源回复标题",
                    },
                    img: {
                        required:icon + "请输入无资源回复图片地址",
                    },
                    url: {
                        required:icon + "请输入无资源回复URL",
                    },
                    lazyload: {
                        required:icon + "请输入选择懒加载模式",
                    },
                },
                submitHandler:function (form) {
                  var fm = new FormData($('#signupForm')[0])
                  $.ajax({
                      type:"post",
                      url:"/action/wconfig",
                      dataType:"json",
                      data:fm,
                      headers: {
                          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                      },
                      processData: false,
                      contentType: false,
                      error:function(){

                      },
                      success:function (resp) {
                        if(resp.status==200){
                            layer.msg(resp.msg)
                        }else{
                            layer.msg(resp.msg)
                        }
                      }
                  })
                }
            });
            $("#cacheForm").validate({
                rules: {
                    timeout: "required",
                },
                messages: {
                    timeout: icon + "请输入全局缓存时间",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#cacheForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/wconfig",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                         layer.msg("修改失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });
            $("#linkForm").validate({
                rules: {
                    lar_link_title: "required",
                    lar_link_href: "required",
                },
                messages: {
                    lar_link_title: icon + "请输入友链标题",
                    lar_link_href: icon + "请输入友链地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#linkForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/addlink",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("添加失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });

            $("#linkForm2").validate({
                rules: {
                    lar_link_title: "required",
                    lar_link_href: "required",
                },
                messages: {
                    lar_link_title: icon + "请输入友链标题",
                    lar_link_href: icon + "请输入友链地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#linkForm2')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/editlink",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("修改失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });

            $("#qqForm").validate({
                rules: {
                    lar_qq_title: "required",
                    lar_qq_href: "required",
                },
                messages: {
                    lar_qq_title: icon + "请输入作品标题",
                    lar_qq_href: icon + "请输入作品地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#qqForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/addqq",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("添加失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });
            $("#qqForm2").validate({
                rules: {
                    lar_qq_title: "required",
                    lar_qq_href: "required",
                },
                messages: {
                    lar_qq_title: icon + "请输入作品标题",
                    lar_qq_href: icon + "请输入作品地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#qqForm2')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/editqq",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("修改失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });


            $("#interfaceForm").validate({
                rules: {
                    lar_link_title: "required",
                    lar_link_href: "required",
                },
                messages: {
                    lar_link_title: icon + "请输入友链标题",
                    lar_link_href: icon + "请输入友链地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#interfaceForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/wconfig",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("添加失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });
            $("#threadForm").validate({
                rules: {
                    lar_link_title: "required",
                    lar_link_href: "required",
                },
                messages: {
                    lar_link_title: icon + "请输入友链标题",
                    lar_link_href: icon + "请输入友链地址",
                },
                submitHandler:function (form) {
                    var fm = new FormData($('#threadForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/wconfig",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("修改失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });
            $("#updateForm").validate({
                rules: {
                    updatetype: "required",
                },
                messages: {
                    updatetype: icon + "请选择更新类型",
                },
                submitHandler:function (form) {
                    layer.msg("更新中");
                    var fm = new FormData($('#updateForm')[0])
                    $.ajax({
                        type:"post",
                        url:"/action/update",
                        dataType:"json",
                        data:fm,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("更新失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                }
            });

            // propose username by combining first- and lastname
            $("#username").focus(function () {
                var firstname = $("#firstname").val();
                var lastname = $("#lastname").val();
                if (firstname && lastname && !this.value) {
                    this.value = firstname + "." + lastname;
                }
            });
            
            $('#clear').click(function () {
                layer.confirm("确认清除全部缓存?",{btn: ['确定', '取消'], title: "laracvs"},function () {
                    $.ajax({
                        type:"post",
                        url:"/action/clearcache",
                        dataType:"json",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                        },
                        processData: false,
                        contentType: false,
                        error:function(){
                            layer.msg("清理失败")
                        },
                        success:function (resp) {
                            if(resp.status==200){
                                layer.msg(resp.msg)
                            }else{
                                layer.msg(resp.msg)
                            }
                        }
                    })
                })
            })
        });
